﻿<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>设置</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../../css/mui.min.css">
		<link rel="stylesheet" href="../../css/base.css" />
		<link rel="stylesheet" href="../../css/app/my/setting.css" />
	</head>

	<body class="mui-fullscreen">
		<!--页面主结构开始-->
		<div id="app" class="mui-views">
			<div class="mui-view">
				<div class="mui-navbar">
				</div>
				<div class="mui-pages">
				</div>
			</div>
		</div>
		<!--页面主结构结束-->
		<!--单页面开始-->
		<div id="setting" class="mui-page">
			<!--页面标题栏开始-->
			<div class="mui-navbar-inner mui-bar mui-bar-nav">
				<button type="button" class="mui-left mui-action-back mui-btn mui-btn-link mui-btn-nav mui-pull-left">
                <span class="mui-icon mui-icon-left-nav"></span>
            </button>
				<h1 class="mui-center mui-title">设置</h1>
			</div>
			<!--页面标题栏结束-->
			<!--页面主内容区开始-->
			<div class="mui-page-content">
				<div class="mui-scroll-wrapper">
					<div class="mui-scroll">
						<ul class="mui-table-view mui-table-view-chevron">
							<li class="mui-table-view-cell">
								<a id="head" href="#setAvatar" class="mui-navigate-right">头像</a>
								<img id="head-img" class="mui-action-preview fr head-img" src="" data-preview-src="" data-preview-group="1"/>
							</li>
							<li class="mui-table-view-cell">
								<a>姓名 <i class="mui-pull-right update" id="userName"></i></a>
							</li>
						</ul>
						<ul class="mui-table-view mui-table-view-chevron">
							<li class="mui-table-view-cell">
								<a href="#resetPwd" class="mui-navigate-right">修改密码</a>
							</li>
						</ul>
						<ul class="mui-table-view mui-table-view-chevron">
							<li class="mui-table-view-cell">
								<a href="#notifications" class="mui-navigate-right">新消息通知<i class="mui-pull-right update"></i></a>
							</li>
							<li class="mui-table-view-cell">
								<a id="clearCache" class="mui-navigate-right">清除缓存<i id="cacheSize" class="mui-pull-right update"></i></a>
							</li>
						</ul>
						<ul class="mui-table-view mui-table-view-chevron">
							<li class="mui-table-view-cell" id="about">
								<a class="mui-navigate-right">关于我们 </a>
							</li>
							<li id="check_update" class="mui-table-view-cell mui-plus-visible">
								<a class="mui-navigate-right">当前版本<i class="mui-pull-right update" id="version"></i></a>
							</li>
						</ul>
						<ul class="mui-table-view">
							<li class="mui-table-view-cell logout" id="logout">
								<a>退出</a>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<!--页面主内容区结束-->
		</div>
		<!--单页面结束-->
		<div id="notifications" class="mui-page">
			<div class="mui-navbar-inner mui-bar mui-bar-nav">
				<button type="button" class="mui-left mui-action-back mui-btn  mui-btn-link mui-btn-nav mui-pull-left">
                <span class="mui-icon mui-icon-left-nav"></span>设置
            </button>
				<h1 class="mui-center mui-title">新消息通知设置</h1>
			</div>
			<div class="mui-page-content">
				<div class="mui-scroll-wrapper">
					<div class="mui-scroll">
						<ul class="mui-table-view">
							<li class="mui-table-view-cell">
								<a>新消息通知<span class="mui-pull-right">已开启</span></a>
							</li>
						</ul>
						<div class="mui-content-padded">
							<p>iphone：请在“设置”——“通知”中进行修改</p>
						</div>
						<ul class="mui-table-view">
							<li class="mui-table-view-cell">
								声音
								<div class="mui-switch mui-switch-blue mui-active mui-switch-mini">
									<div class="mui-switch-handle"></div>
								</div>
							</li>
							<li class="mui-table-view-cell">
								震动
								<div class="mui-switch mui-switch-blue mui-active mui-switch-mini">
									<div class="mui-switch-handle"></div>
								</div>
							</li>
						</ul>
						<div class="mui-content-padded">
							<p>当程序运行时，你可以设置是否需要声音或者震动</p>
						</div>
						<div></div>
					</div>
				</div>
			</div>
		</div>
		<div id="resetPwd" class="mui-page">
			<div class="mui-navbar-inner mui-bar mui-bar-nav">
				<button type="button" class="mui-left mui-action-back mui-btn  mui-btn-link mui-btn-nav mui-pull-left">
                <span class="mui-icon mui-icon-left-nav"></span>设置
            </button>
				<h1 class="mui-center mui-title">修改密码</h1>
			</div>
			<div class="mui-page-content">
				<div class="mui-scroll-wrapper">
					<div class="mui-scroll">
						<div class="mui-content-padded" style="margin: 0px;margin-top: 14px">
							<form class="mui-input-group">
								<div class="mui-input-row">
									<input type="password" class="mui-input-password" maxlength="20" placeholder="请输入当前密码" v-model="oldPwd">
								</div>
								<div class="mui-input-row">
									<input type="password" class="mui-input-password" maxlength="20" placeholder="请输入新密码" v-model="newPwd">
								</div>
							</form>
						</div>
						<div v-show="msg==''" class="mui-input-row div-info">6~20位字母数字组成</div>
						<div v-show="msg!=''" class="mui-input-row div-info error" v-text="msg"></div>
						<div class="div-submit" style="margin-top: 30px;">
							<button type="button" class="btnSubmit mui-btn next-btn mui-btn-primary" v-on:tap="doRestPwd()" v-bind:disabled="isCanSubmit">提交</button>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script src="../../js/mui.min.js"></script>
		<script src="../../js//libs/mui.view.js"></script>
		<script type="text/javascript" src="../../js/common/config.js"></script>
        <script src="../../js/common/webSql.js"></script>
        <script src="../../js/libs/mock-min.js"></script>
		<script type="text/javascript" src="../../js/common/global.js"></script>
        <script src="../../js/common/mockdata.js"></script>
		<script type="text/javascript" src="../../js/libs/vue.min.js"></script>
		<script type="text/javascript" src="../../js/libs/md5.min.js"></script>
        <script src="../../js/app/my/appupdate.js"></script>
        <script src="../../js/app/my/my.js"></script>
		<script>
            var openerWV = null;
            mui.init();
			var btnArray = ['取消', '确定'];
			var Intent = null,
				File = null,
				Uri = null,
				main = null;
				
			//修改密码
			var pwdApp = new Vue({
				el: '#resetPwd',
				data: {
					oldPwd: '',
					newPwd: '',
					msg: ''
				},
				computed: {
					isCanSubmit: function() {
						if(this.oldPwd == '' && this.newPwd == '') {
							this.msg = '';
							return true;
						}
						var _length = this.oldPwd.length >= 6 && this.newPwd.length >= 6;
						if(!_length) {
							this.msg = '密码必须6位数以上';
							return true;
						}
						console.log('当前密码:' + localStorage.getItem('$smp_cur_pwd'));
						console.log('旧密码：' + this.oldPwd)
						var _oldPwd = md5(this.oldPwd);
						var _pwdVerify = localStorage.getItem('$smp_cur_pwd') == _oldPwd;
						if(!_pwdVerify) {
							this.msg = '旧密码输入错误';
							return true;
						}
						console.log(_length + ',' + _pwdVerify)
						return false;
					}
				},
				methods: {
					doRestPwd: function() {					  
						pwdApp.newPwd = md5(pwdApp.newPwd);						 
						g.ajax(config.ModifyPassword, {
							data: {uid:config.USER_ID,password:pwdApp.newPwd,muid:config.USER_ID} ,
							success: function(data) {
								if(data && data.Data == 1) {
									mui.toast('密码修改成功');
									pwdApp.oldPwd='';
									pwdApp.newPwd='';
									mui.back();
								}
							}
						});

					}
				}
			})

			mui.ready(function() {
				var user = g.getUserInfo();
				if(user) {
					document.getElementById("userName").innerText = user.NAME;
				}
				eventListener();
			});
			mui.plusReady(function() {
			    var wv = plus.webview.currentWebview();
			    openerWV = wv.opener();
			    // 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, function (inf) {
				    g.id("version").innerHTML = 'V' + inf.version;
				});
				g.id("userName").innerText = wv.userName;
				var _imgUrl = wv.imgUrl.indexOf('tab_') > 0 ? '../' + wv.imgUrl : wv.imgUrl;
				console.log(_imgUrl);
				if (_imgUrl) {
				    document.getElementById("head-img").src = _imgUrl;
				} else {
				    g.defaultImg("head-img",null, _imgUrl);
				}
				setTimeout(function() {
					g.initImgPreview("#head-img");
				}, 300);
				//获取缓存大小
				var _result = '';
				plus.cache.calculate(function(size) {
					_result = g.bytesToSize(size);
					console.log(_result)
					document.getElementById("cacheSize").innerText =_result;
				});
				if(mui.os.stream) {
					document.getElementById("check_update").display = "none";
				}
				//				if(mui.os.android) {
				//					main = plus.android.runtimeMainActivity();
				//					Intent = plus.android.importClass("android.content.Intent");
				//					File = plus.android.importClass("java.io.File");
				//					Uri = plus.android.importClass("android.net.Uri");
				//					var _size = calcCache4Android();
				//					_result = g.bytesToSize(_size);
				//				} else {
				//					plus.cache.calculate(function(size) {
				//						_result = g.bytesToSize(size);
				//					});
				//				}
			})
			//初始化单页view
			var viewApi = mui('#app').view({
				defaultPage: '#setting'
			});
			var view = viewApi.view;
			(function($) {
				//处理view的后退与webview后退
				var oldBack = $.back;
				$.back = function() {
					if(viewApi.canBack()) { //如果view可以后退，则执行view的后退
						viewApi.back();
					} else { //执行webview后退
						oldBack();
					}
				};
			})(mui);
			//初始化单页的区域滚动
			mui('.mui-scroll-wrapper').scroll();

			function calcCache4Android() {
				var cacheSize = 0;
				console.log("start calc android");
				var sdRoot = main.getCacheDir();
				var files = plus.android.invoke(sdRoot, "listFiles");
				cacheSize += getFolderSize(files);
				console.log("android size-->" + cacheSize);
				return cacheSize;
			}

			function getFolderSize(files) {
				var size = 0;
				var len = files.length;
				for(var i = 0; i < len; i++) {
					// 如果下面还有文件
					if(files[i].isDirectory()) {
						size = size + getFolderSize(files[i]);
					} else if(!files[i].isHidden()) {
						size = size + files[i].length();
					}
				}
				return size;
			}

			function eventListener() {
				//关于我们
				document.getElementById('about').addEventListener('tap', function() {
					g.openWindowWithTitle({
						url: 'about.html',
						id: 'about'
					}, '关于我们');
				})
				//清除缓存
				document.getElementById('clearCache').addEventListener('tap', function() {
					mui.confirm('图片及离线缓存的内容将会被删除', '确定删除所有缓存？', btnArray, function(e) {
						if(e.index == 1) {
							plus.cache.clear(function() {
								mui.toast("应用缓存清除成功!");
							});
						}
					});
				});
				//退出登录
				document.getElementById("logout").addEventListener('tap', function() {
					mui.confirm('确定退出登录吗？', '退出确认', btnArray, function(e) {
						if(e.index == 1) {
							g.logout();
						}
					});
				});
				//检查更新
				document.getElementById("check_update").addEventListener('tap', function() {
				    appUpdate(true);
				});
				//更换头像
				mui(".mui-table-view-cell").on("tap", "#head", function(e) {
					if(mui.os.plus) {
						var a = [{
							title: "拍照"
						}, {
							title: "从手机相册选择"
						}];
						plus.nativeUI.actionSheet({
							title: "修改头像",
							cancel: "取消",
							buttons: a
						}, function(b) {
							switch(b.index) {
								case 0:
									break;
								case 1:
								    getImage();
									break;
								case 2:
								    galleryImg();
									break;
								default:
									break
							}
						})
					}
				});
			}
			//拍照
			function getImage() {
				var c = plus.camera.getCamera();
				c.captureImage(function(e) {
					plus.io.resolveLocalFileSystemURL(e, function(entry) {
						var s = entry.toLocalURL() + "?version=" + new Date().getTime();
						console.log(s);
						compressImage(entry.toLocalURL(), entry.name);
						document.getElementById("head-img").src = s;
					    //刷新父头像 refreshHeadImg()
						if (openerWV) {
						    openerWV.evalJS("refreshHeadImg()");
						}
					}, function(e) {
						console.log("读取拍照文件错误：" + e.message);
					});
				}, function(s) {
					console.log("error" + s);
				}, {
					filename: "_doc/head.jpg"
				})
			}
			//从相册中选择
			function galleryImg() {
			    plus.gallery.pick(function (a) {
			        plus.io.resolveLocalFileSystemURL(a, function (entry) {
			            compressImage(entry.toLocalURL(), entry.name);
			            plus.io.resolveLocalFileSystemURL("_doc/", function (root) {
			                root.getFile("head.jpg", {}, function (file) {
			                    //文件已存在
			                    file.remove(function () {
			                        console.log("file remove success");
			                        entry.copyTo(root, 'head.jpg', function (e) {
			                            var e = e.fullPath + "?version=" + new Date().getTime();
			                            document.getElementById("head-img").src = e;
			                            //变更大图预览的src
			                            //目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
			                            document.querySelector("#__mui-imageview__group .mui-slider-item img").src = e + "?version=" + new Date().getTime();
			                        },
										function (e) {
										    console.log('copy image fail:' + e.message);
										});
			                    }, function () {
			                        console.log("delete image fail:" + e.message);
			                    });
			                    //刷新父头像
			                    if (openerWV) {
			                        openerWV.evalJS("refreshHeadImg()");
			                    }
			                }, function () {
			                    //文件不存在
			                    entry.copyTo(root, 'head.jpg', function (e) {
			                        var path = e.fullPath + "?version=" + new Date().getTime();
			                        document.getElementById("head-img").src = path;
			                        //变更大图预览的src
			                        //目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
			                        document.querySelector("#__mui-imageview__group .mui-slider-item img").src = path;
			                    },
									function (e) {
									    console.log('copy image fail:' + e.message);
									});
			                    //刷新父头像
			                    if (openerWV) {
			                        openerWV.evalJS("refreshHeadImg()");
			                    }
			                });
			            }, function (e) {
			                console.log("get _www folder fail");
			            })
			        }, function (e) {
			            console.log("读取拍照文件错误：" + e.message);
			        });
			    }, function (a) { }, {
			        filter: "image"
			    })
			};
		</script>
	</body>

</html>